﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace MaterialEditor
{
    public class AddNode : Node
    {
        private InputSlot A
        {
            get { return input[0]; }
        }
        private InputSlot B
        {
            get { return input[1]; }
        }

        private NodeValue _result;
        public override NodeValue Result
        {
            get { return _result; }
        }

        private NodeValueType[][] _acceptTypes;
        public override NodeValueType[][] acceptTypes
        {
            get { return _acceptTypes; }
        }

        public AddNode(int x, int y)
            : base(x, y)
        {
            input = new InputSlot[2]
            {
                new InputSlot(this, null, "A"),
                new InputSlot(this, null, "B"),
            };
            

            A.Connected += A_Connected;
            B.Connected += B_Connected;

            A.Disconnected += A_Disconnected;
            B.Disconnected += B_Disconnected;

            A.ValueChanged += A_Connected;
            B.ValueChanged += B_Connected;

            _acceptTypes = new NodeValueType[][]
            {
                new NodeValueType[] { NodeValueType.ValueColor, NodeValueType.ValueFloat, NodeValueType.ValueTexture, NodeValueType.ValueVector2, NodeValueType.ValueVector3 },
                new NodeValueType[] { NodeValueType.ValueColor, NodeValueType.ValueFloat, NodeValueType.ValueTexture, NodeValueType.ValueVector2, NodeValueType.ValueVector3 },
            };

            output = new InputSlot(this, null,  "");

            Name = "Add";
        }

        void B_Disconnected()
        {
            if (A.connector != null)
                _result = A.connector.startSlot.value;
            else
                _result = null;
        }

        void A_Disconnected()
        {
            if (B.connector != null)
                _result = B.connector.startSlot.value;
            else
                _result = null;
        }

        void B_Connected()
        {
            try
            {
                if (A.connector != null)
                    _result = A.connector.startSlot.value + B.connector.startSlot.value;
                else
                    _result = B.value;
            }
            catch (InvalidOperationException e)
            {
                B.connector.Disconnect();
            }
        }

        void A_Connected()
        {
            try
            {
                if (B.connector != null)
                    _result = A.connector.startSlot.value + B.connector.startSlot.value;
                else
                    _result = A.value;
            }
            catch (InvalidOperationException e)
            {
                A.connector.Disconnect();
            }
        }

        public override object Clone()
        {
            AddNode n = new AddNode((int)position.X, (int)position.Y);
            if (_result != null)
                n._result = new NodeValue(_result.Value);

            return n;
        }

        public override void Draw()
        {
            
            base.Draw();
        }
    }
}
